/*
 *	MP Objects
 *
 *	Author:	S.C.Merrall
 *
 *	File:	mp_object.m
 *
 *	Contents:	error
 *			destroy_mp_object
 *
 *	Description:	I am attempting to write this code is an object
 *                      orientated fashion. Objects in the system are 
 *                      always of the form make_OBJECT and these objects
 *                      are accessed via functions from this file. They
 *                      are all macros but this should give a nice 
 *                      generic feel to it.
 *                         The function error handles exceptions caused by 
 *                      applying functions to objects of the wrong type or
 *                      null objects. It is wrapped around the variable 
 *                      being dereferenced within the Macro used for 
 *                      accessing the appropriate slot.
 *
 *	Change History:
 *
 *	Date   Name Comment
 *    -------- ---- -------
 *    04:02:91 SCM  Created
 *    18:02:91 SCM  Proper naming convention plus generic accessors
 *    19:02:91 SCM  Added destroy_mp_object
 *    28:08:91 SCM  Combined free and type into 32 bit word
 *
 */

#include <stdio.h>

#include "mp_object.h"
#include "mp_debug_off.h"

#ifdef __STDC__

object error( object OBJECT, char *f_name, int expected_type )

#else

object error( OBJECT, f_name, expected_type )

object OBJECT;
char *f_name;
int expected_type;

#endif

{
  if (OBJECT == NULL)
    {
      fprintf(stderr, "ERROR: %s applied to null object in %s\n",
	      f_name,dbg_g_fname);
      exit(1);
    }

  if (expected_type == OI_make) return OBJECT;
  if (OM_freep(OBJECT))
    {
      fprintf(stderr, "ERROR: %s applied to freed object type %d in %s\n",
	      f_name, OM_type(OBJECT), dbg_g_fname);
      exit(1);
    }

  if ((OM_type(OBJECT) & OD_DYNAMIC) == OD_DYNAMIC) {

    if (strcmp(f_name,"OF_destroy") == 0) {
      
      fprintf(stderr,"ERROR: OF_destroy applied to dynamic class %x, in %s\n",
	      OM_type(OBJECT)-OD_DYNAMIC, dbg_g_fname);
      exit(1);
    }
  }


  if (expected_type == OI_Generic) return OBJECT;


  if ((OM_type(OBJECT)  & expected_type) == 0)
    {
    fprintf(stderr,"ERROR: %s applied to class %x, expected class %x in %s\n",
	      f_name, OM_type(OBJECT), expected_type, dbg_g_fname );
      exit(1);
    }

  return OBJECT;

}

#ifdef __STDC__

visible int destroy_mp_object( object G_to_be_destroyed )

#else

visible int destroy_mp_object( G_to_be_destroyed )

object G_to_be_destroyed;

#endif

{
  return OF_destroy(G_to_be_destroyed);
} 


#ifdef __STDC__

object no_constructor( void  )

#else

object no_constructor( )

#endif

{
  fprintf(stderr,"ERROR: No constructor, attempted from %s\n", dbg_g_fname);
  exit(1);
}
